package com.yulongtian.offers.day202209.day20220923;

import java.util.HashMap;

/**
 * https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/
 * @author YulongTian
 * @date 2022/9/23 - 16:48
 */
public class Test05 {
    public static void main(String[] args) {
        int[] nums={1, 2, 3, 2, 2, 2, 5, 4, 2};
        System.out.println(majorityElement(nums));
    }

    public static int majorityElement(int[] nums) {
        HashMap<Integer,Integer> map=new HashMap<>();

        int len=nums.length;
        if(len==1){
            return nums[0];
        }
        map.put(nums[0],1);
        for(int i=1;i<len;i++){
            if(map.get(nums[i])==null){
                map.put(nums[i],1);
            }else{
                int temp=map.get(nums[i])+1;
                map.put(nums[i],temp);
                if(map.get(nums[i])>len/2){
                    return nums[i];
                }
            }
        }


        return 0;
    }

}
